#include "PrecompiledHeader_Engineer.h"
#include "WBullets.h"

/*--------------------------------------------------------------------------*/

WBullets::WBullets()
{

}

/*--------------------------------------------------------------------------*/

WBullets::~WBullets()
{

}

/*--------------------------------------------------------------------------*/

void WBullets::AddBullet(const glm::vec3& pos, const glm::vec3& dir, float radius, const Color& color)
{
	this->m_bullets.push_back(new IBullet(pos, dir, radius, color));
}

/*--------------------------------------------------------------------------*/

void WBullets::OnLevelLoad(const std::string& level)
{

}

/*--------------------------------------------------------------------------*/

void WBullets::OnLevelUnload()
{
	for (std::list<IBullet*>::iterator i = this->m_bullets.begin(); i != this->m_bullets.end(); ++i)
	{
		CTree::getInstance()->DeleteObject(*i);
	}
}

/*--------------------------------------------------------------------------*/

void WBullets::OnLevelCalc()
{
	for (std::list<IBullet*>::iterator b = this->m_bullets.begin(); b != this->m_bullets.end();)
	{
		(*b)->Update();

		if ((*b)->GetFrustumTestResult() == false)
		{
			if ((*b)->GetParent() == NULL)
			{
				b = this->m_bullets.erase(b);
				continue;
			}

			(*b)->RemoveFromTree();
		}

		++b;
	}

	std::list<IMob*>& mobs = WMobs::getInstance()->GetMobs();

	for (std::list<IBullet*>::iterator b = this->m_bullets.begin(); b != this->m_bullets.end(); ++b)
	{
		for (std::list<IMob*>::iterator m = mobs.begin(); m != mobs.end(); ++m)
		{
			if (glm::distance((*b)->GetPosition(), (*m)->GetPosition()) < 3.0f)
			{
				(*m)->Kill();
				std::stringstream ss;
				ss << "print('" << *b << " killed " << *m << "')";
				CShell::getInstance()->Execute(ss.str());
			}
		}
	}
}

/*--------------------------------------------------------------------------*/

std::list<IBullet*> WBullets::GetBullets()
{
	return this->m_bullets;
}

/*--------------------------------------------------------------------------*/
